package player;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import utilPackage.Commons;

import main.Sprite;

public class Player extends Sprite implements Commons{

    private final int START_Y = 400; 
    private final int START_X = 300;

    private final String player = "../Image/player.gif";
    public final String expl = "../Image/player.gif";
    private int power;
    private double speed;
    private boolean force_field = false;
    public boolean hammer_bool = false;
    public Hammer hammer;
    public ArrayList move_history;    
    public int player_shot_animate;
    
    public Player() {

    	player_shot_animate = 0;
        ImageIcon ii = new ImageIcon(this.getClass().getResource(player));

        width = ii.getImage().getWidth(null);
        height = ii.getImage().getHeight(null); 
        this.power = 3;
        this.speed = 4;
        collided = false;

        setImage(ii.getImage());
        setX(BOARD_WIDTH - (BOARD_WIDTH/2));
        setY(BOARD_HEIGTH - (BOARD_WIDTH/3));
        move_history = new ArrayList();
        visible = true;
    }
    
    /*public boolean doesCollide(Sprite s) {
		int sLeftEdge = s.getX();
		int sRightEdge = s.getX()+ s.getWidth();
		int sTopEdge = s.getY();
		int sBottomEdge = s.getY() + s.getHeight();
		
		int leftEdge = this.x;
		int rightEdge = this.x + this.width;
		int topEdge = this.y;
		int bottomEdge = this.y + this.height;
		
		if( sLeftEdge <= rightEdge && 
		    sRightEdge >=  leftEdge &&
		    sTopEdge <= bottomEdge &&
		    sBottomEdge >= topEdge ) {
				s.setDying(true);
				setDying(true);
				return true;
		}
		return false;
	}*/

    public void think() {
        
      this.move();
    }
    
    public void draw(){
    	int q = PLAYER_WIDTH/4;
    	if(!collided) {
	    	g.setColor(Color.white);
			Font small = new Font("Helvetica", Font.BOLD, 10);
			g.setFont(small);
			g.drawString("Not Collided", x + 10, y);
    	}
    	else {
    		g.setColor(Color.white);
			Font small = new Font("Helvetica", Font.BOLD, 10);
			g.setFont(small);
			g.drawString("Collided", x + 10, y);
    	}
    	if(player_shot_animate > 0){
    		this.g.setColor(new Color(255, 255, 255, 2*player_shot_animate));
    		this.g.drawRoundRect(this.x -player_shot_animate -10 , this.y-40 -(player_shot_animate)- 4,player_shot_animate*2 + PLAYER_WIDTH +20 ,(player_shot_animate*2+30), 30, 30);
    		this.g.fillRect(this.x -player_shot_animate -10, this.y-40 - (player_shot_animate) - 2,player_shot_animate*2 + PLAYER_WIDTH +20 , (player_shot_animate)*2+30);
    			
    		this.g.setColor(new Color(255, 255, 255, 1*player_shot_animate));
    		this.g.drawRoundRect(this.x -player_shot_animate -3 , this.y-30 -(player_shot_animate)- 4,player_shot_animate*2 + PLAYER_WIDTH +6 ,(player_shot_animate*2+30), 20, 20);
    		this.g.fillRect(this.x -player_shot_animate -3, this.y-30 - (player_shot_animate) - 2,player_shot_animate*2 + PLAYER_WIDTH +6 , (player_shot_animate)*2+30);
    			
    		this.g.setColor(new Color(255, 255, 255, 1*player_shot_animate));
    		this.g.drawRoundRect(this.x -player_shot_animate , this.y-20 -(player_shot_animate)- 4,player_shot_animate*2 + PLAYER_WIDTH ,(player_shot_animate*2+20), 20, 20);
    		this.g.fillRect(this.x -player_shot_animate , this.y-20 - (player_shot_animate) - 2,player_shot_animate*2 + PLAYER_WIDTH , (player_shot_animate)*2+20);
    		    			
    		this.g.setColor(new Color(255, 255, 255, 1*player_shot_animate));
    		this.g.drawRoundRect(this.x -player_shot_animate , this.y-6 -(player_shot_animate)- 4,player_shot_animate*2 + PLAYER_WIDTH ,(player_shot_animate*2+10), 20, 20);
    		this.g.fillRect(this.x -player_shot_animate , this.y-6 - (player_shot_animate) - 2,player_shot_animate*2 + PLAYER_WIDTH , (player_shot_animate)*2+10);
    		
    		this.g.setColor(new Color(0, 0, 255, 20*player_shot_animate));
    		this.g.drawRect(this.x+q -player_shot_animate +1 -5, this.y+7 -(player_shot_animate)- 4,player_shot_animate*2 +10,(player_shot_animate*2));
    		this.g.fillRect(this.x+q -player_shot_animate +1 -5, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2 +10, (player_shot_animate));
    		  			
    		this.g.setColor(new Color(255, 255, 255, 10*player_shot_animate));
    		this.g.fillRect(this.x+q -player_shot_animate +1, this.y+7 -(player_shot_animate)- 4,player_shot_animate*2 ,(player_shot_animate*2));
    		this.g.fillRect(this.x+q -player_shot_animate +1, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2 , (player_shot_animate));
    			
    		this.g.setColor(new Color(255, 255, 255, 255));
    		this.g.fillRect(this.x+q -player_shot_animate +5, this.y+7 -(player_shot_animate)- 4,player_shot_animate*2 -8,(player_shot_animate*2));
    		this.g.fillRect(this.x+q -player_shot_animate +5, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2 -8, (player_shot_animate));
    		    			
    		this.g.setColor(new Color(0, 0, 255, 20*player_shot_animate));			
    		this.g.drawRect(this.x+(q*3) -player_shot_animate+1 -5, this.y+7-(player_shot_animate)- 4,player_shot_animate*2 +10,(player_shot_animate*2));
    		this.g.fillRect(this.x+(q*3) -player_shot_animate +1 -5, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2 +10 , (player_shot_animate));
    			
    			
    		this.g.setColor(new Color(255, 255, 255, 10*player_shot_animate));
    		this.g.fillRect(this.x+(q*3) -player_shot_animate+1, this.y+7-(player_shot_animate)- 4,player_shot_animate*2 ,(player_shot_animate*2));
    		this.g.fillRect(this.x+(q*3) -player_shot_animate +1, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2 , (player_shot_animate));
    			
    			
    		this.g.setColor(new Color(255, 255, 255, 255));
    		this.g.fillRect(this.x+(q*3) -player_shot_animate+5, this.y+7-(player_shot_animate)- 4,player_shot_animate*2 -8,(player_shot_animate*2));
    		this.g.fillRect(this.x+(q*3) -player_shot_animate +5, this.y+7 - (player_shot_animate) - 2,player_shot_animate*2-8 , (player_shot_animate));
    			
    			
    		player_shot_animate -=1;
    			
    		}
    		//if( player.hammer_bool == true){
    		//	player.hammer.drawHammer(g, energy);
    		//}
    		//drawPlayerTail(g);
    		//drawForceField(g);
//
    		//drawForceField(g);
    		if (this.isVisible()) {
    			g.drawImage(this.getImage(), this.x, this.y, this.imageObserver );
    		}
    		if (this.isDying()) {
    			this.die();    			
    		}
    }
    
    
    public int getPower() {
        
        return this.power;
      }
    
    public void setPower(int x ) {
    	power = x;
    }
    
    public double getSpeed() {
        
        return this.speed;
      }
    
    public void setSpeed (double x ) {
        speed = x;
    }
    
    
    public void setForceField(boolean x ) {
        force_field = x;
    }
    
    public boolean getForceField() {
        return force_field;
    }
    
    public void move() {
        
       int old_x = x;
    	int old_y = y;
        if(Math.abs(dx) >= MAX_PLAYER_SPEED)dx =(int)Math.copySign(MAX_PLAYER_SPEED, dx);
        if(Math.abs(dy) >= MAX_PLAYER_SPEED)dy =(int) Math.copySign(MAX_PLAYER_SPEED, dy);    
        
        
        x += (int)dx;
        y += (int)dy;        
        
        if(x < 2){
            x = 2;
        }
        if(x >= BOARD_WIDTH - width){
            x = BOARD_WIDTH - width;
        }
        if(y < 2){
            y = 2;
        }
        if(y >= BOARD_HEIGTH){
            y = BOARD_HEIGTH - 40;
        }
  if(old_y > y){
        	
        	/*Sprite s = new Sprite();
        	s.setLife(PLAYER_TAIL);
        	s.setX(x);
        	s.setY(y);
        	move_history.add(s);  */ 	
        	
        }
    }



    public void keyPressed(String key) {
                
        
        if (key == "VK_LEFT")
        {
            dx = -(int)speed; 
            //heading = - (int)speed;
        }
        
        if (key == "VK_UP")
        {
        	//acceleration += 0.1; 
        	dy =  -(int)(speed ); 
            
            
        }
        if (key == "VK_DOWN")
        {
        	//acceleration -= 0.1; 
        	dy = (int)(speed ) ; 
            
            
        }

        if (key == "VK_RIGHT")
        {
            dx = (int)speed; 
            //heading = 2;
        }
        if (key == "VK_SPACE")
        {
        	player_shot_animate = 5;
        }
        

    }
    

  public void keyReleased(String key) {
	  if (key == "VK_LEFT")
      {
          dx = 0; 
          //heading = - (int)speed;
      }
      
      if (key == "VK_UP")
      {
      	//acceleration += 0.1; 
      	dy =  0; 
          
          
      }
      if (key == "VK_DOWN")
      {
      	//acceleration -= 0.1; 
      	dy = 0 ; 
          
          
      }

      if (key == "VK_RIGHT")
      {
          dx = 0; 
          //heading = -2;
      }
    }

@Override
public boolean doesCollide(Sprite s) {
	// TODO Auto-generated method stub
	return false;
}
}
